/*
 * This program is free software: you can redistribute it and/or modify it under the terms of the 
 * GNU Affero General Public License as published by the Free Software Foundation, either version 3 
 * of the License, or (at your option) any later version.
 * 
 * In accordance with Section 7(b) of the GNU Affero General Public License version 3, 
 * the Appropriate Legal Notices must display the following words on all interactive user interfaces: 
 * "Copyright (C) 2005-2011 SplendidCRM Software, Inc. All rights reserved."
 */

var MAX_DUMP_DEPTH = 2;

function dumpObj(obj, name, indent, depth)
{
	if ( indent == undefined )
		indent = '';
	if ( depth == undefined )
		depth = 0;
	if ( depth > MAX_DUMP_DEPTH )
	{
		return indent + name + ': <Maximum Depth Reached>\n';
	}
	if ( typeof obj == 'object' )
	{
		var child = null;
		var output = indent + name + '\n';
		indent += '\t';
		for ( var item in obj )
		{
			try
			{
				child = obj[item];
			}
			catch (e)
			{
				child = '<Unable to Evaluate>';
			}
			if ( typeof child == 'object' )
			{
				output += dumpObj(child, item, indent, depth + 1);
			}
			else
			{
				output += indent + item + ': ' + child + '\n';
			}
		}
		return output;
	}
	else
	{
		return obj;
	}
}

function Right(str, n)
{
	if ( n <= 0 )
		return '';
	else if ( n > String(str).length )
		return str;
	else
	{
		var iLen = String(str).length;
		return String(str).substring(iLen, iLen - n);
	}
}

function Left(str, n)
{
	if ( n <= 0 )
		return '';
	else if ( n > String(str).length )
		return str;
	else
	{
		return String(str).substring(0, n);
	}
}

function StartsWith(str1, str2)
{
	return Left(str1, str2.length) == str2;
}

function Trim(str)
{
	return str.replace(/^\s\s*/, '').replace(/\s\s*$/, '');
}

function getQuerystring(key, default_)
{
	if ( default_==null )
		default_ = '';
	key = key.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
	var regex = new RegExp("[\\?&]"+key+"=([^&#]*)");
	var qs = regex.exec(window.location.href);
	if ( qs == null )
		return default_;
	else
		return qs[1];
}

function parseEmailHeaders(msg)
{
	var arrHeaders = new Array();
	try
	{
		var lines = msg.split('\n');
		var sLastFieldName = null;
		for ( var i = 0; i < lines.length; i++ )
		{
			var line = lines[i];
			if ( line.length == 0 )
			{
				break;
			}
			else if ( line.charAt(0) != ' ' )
			{
				var nFieldEnd   = line.indexOf(':');
				if ( nFieldEnd > 0 )
				{
					var sFieldName  = line.substring(0, nFieldEnd);
					var sFieldValue = line.substring(nFieldEnd + 1);
					arrHeaders[sFieldName] = sFieldValue;
					sLastFieldName = sFieldName;
				}
			}
			else if ( sLastFieldName != null )
			{
				arrHeaders[sLastFieldName] += '\n' + line;
			}
		}
	}
	catch(e)
	{
	}
	return arrHeaders;
}

// http://www.ejball.com/EdAtWork/2005/03/31/JavaScriptBindingFunctionArguments.aspx
function BindArguments(fn)
{
	var args = [];
	for ( var n = 1; n < arguments.length; n++ )
		args.push(arguments[n]);
	return function () { return fn.apply(this, args); };
}

function RegisterEnterKeyPress(e, sSubmitID)
{
	if ( e != null )
	{
		if ( e.which == 13 )
		{
			var btnSubmit = document.getElementById(sSubmitID);
			if ( btnSubmit != null )
				btnSubmit.click();
			return false;
		}
	}
	else if ( event != null )
	{
		if ( event.keyCode == 13 )
		{
			event.returnValue = false;
			event.cancel = true;
			var btnSubmit = document.getElementById(sSubmitID);
			if ( btnSubmit != null )
				btnSubmit.click();
		}
	}
}

function checkAll(sFieldID, value)
{
	var fld = document.getElementsByName(sFieldID);
	for (var i = 0; i < fld.length; i++)
	{
		if ( fld[i].type == 'checkbox' )
			fld[i].checked = value;
	}
}


